package com.intretech.umsin.code.generator.template;


import com.intretech.umsin.code.generator.component.CamelCaseUtils;
import com.intretech.umsin.code.generator.component.TemplateHelper;
import com.intretech.umsin.code.generator.component.TemplateType;
import com.intretech.umsin.code.generator.provider.SchemaProviderFactory;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * @version V1.0
 * @ClassName: GeneratorVoMapperTemplate
 * @Description:
 * @author: 李启联
 * @date: 2020-03-25 09:57
 * @Copyright: Copyright (c), 2020
 */
@ConfigurationProperties(prefix = "vo-mapper-template", ignoreUnknownFields = false)
@PropertySource("classpath:template.properties")
@Component
public class GeneratorVoMapperTemplate {

    @Autowired
    private SchemaProviderFactory schemaProviderFactory;

    @Autowired
    private Configuration config;

    @Autowired
    private TemplateHelper templateHelper;

    private String templateName;
    private String packageName;
    private String imports;
    private String outputFilePath;

    public String getTemplateName() {
        return templateName;
    }

    public void setTemplateName(String templateName) {
        this.templateName = templateName;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getImports() {
        return imports;
    }

    public void setImports(String imports) {
        this.imports = imports;
    }

    public String getOutputFilePath() {
        return outputFilePath;
    }

    public void setOutputFilePath(String outputFilePath) {
        this.outputFilePath = outputFilePath;
    }

    public void renderToFile(String sourceTable, Map<String, Object> dataModel) throws Exception {
        if(null == dataModel || dataModel.size()<1)//如果未提供数据模型则创建数据模型
            dataModel = schemaProviderFactory.createDbSchemaProvider().prepareDataModel(sourceTable);

        Template template = config.getTemplate(this.getTemplateName());

        File file = new File(this.getOutputFilePath()+"\\"+ CamelCaseUtils.underlineToCamel(sourceTable.toLowerCase(),true)+"VoEntityConverter.java");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        FileWriter fw = new FileWriter(file);

        dataModel.put("imports", Arrays.asList(imports.split(";")));
        dataModel.put("packageName", this.getPackageName());

        template.process(dataModel, fw);
        fw.close();
        //dataModel.clear();
    }

    public void renderToZip(String basePackage, String sourceTable, Map<String, Object> dataModel , ZipOutputStream zip) throws Exception{
        String className = CamelCaseUtils.underlineToCamel(sourceTable.toLowerCase(),true)+"VoMapper";
        if(null == dataModel || dataModel.size()<1)//如果未提供数据模型则创建数据模型
            dataModel = schemaProviderFactory.createDbSchemaProvider().prepareDataModel(sourceTable);

        dataModel.put("imports", Arrays.asList(imports.split(";")));
        dataModel.put("packageName", basePackage+".vo.mapper");
        templateHelper.renderToZip(schemaProviderFactory, TemplateType.VoMapperTemplate,basePackage,sourceTable,className,this.getTemplateName(),dataModel,zip);
    }
}